package com.scau.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.dao.TeacherDao;
import com.scau.pojo.Teacher;
import com.scau.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService{
	@Autowired
	TeacherDao teacherDAO;

	@Override
	public List<Teacher> query() {
		return teacherDAO.query();
	}

	@Override
	public int add(JSONObject jsonObject) {
		Teacher teacher=new Teacher();
		String tName=jsonObject.getString("name");
		String tNickname=jsonObject.getString("nickname");
		String tPassword=jsonObject.getString("password");
		String tSex=jsonObject.getString("sex");
		String tTel=jsonObject.getString("tel");
		teacher.settName(tName);
		teacher.settNickname(tNickname);
		teacher.settPassword(tPassword);
		teacher.settSex(tSex);
		teacher.settTel(tTel);
		return teacherDAO.add(teacher);
	}

	@Override
	public int delete(JSONArray jsonArray){
		int result=0;
		List<Integer> idArr=JSON.parseArray(jsonArray.toString(), Integer.class);
		try {
			for(int id : idArr){
				if(teacherDAO.deleteMulti(id)!=1){
					break;
					
				}else{
					result=1;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	@Override
	public int update(JSONObject jsonObject) {
		int result=0;
		Teacher teacher=new Teacher();
		int tId=jsonObject.getInteger("id");
		String tName=jsonObject.getString("name");
		String tNickname=jsonObject.getString("nickname");
		String tPassword=jsonObject.getString("password");
		String tSex=jsonObject.getString("sex");
		String tTel=jsonObject.getString("tel");
		teacher.settId(tId);
		teacher.settName(tName);
		teacher.settName(tNickname);
		teacher.settPassword(tPassword);
		teacher.settSex(tSex);
		teacher.settTel(tTel);
		try {
			result=teacherDAO.update(teacher);
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return result;
	}

	
}
